package com.yuehuanghun.admin.core.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.yuehuanghun.framework.web.ApiExceptionAdvice;
import com.yuehuanghun.framework.web.RequestContextExceptionHolder;
import com.yuehuanghun.framework.web.ReturnWrapper;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.exception.SaTokenException;

@SuppressWarnings("rawtypes")
@RestControllerAdvice
public class GlobalExceptionAdvice extends ApiExceptionAdvice {
	
	@ExceptionHandler(NotLoginException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
	public ReturnWrapper handleNotLoginException(NotLoginException ex) {
		logger.debug("", ex);
        RequestContextExceptionHolder.setException(ex);
        return makeResult(HttpStatus.UNAUTHORIZED.value(), "尚未登录或登录已超时", null);
	}
	
	@ExceptionHandler(NotPermissionException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
	public ReturnWrapper handleNotPermissionException(NotPermissionException ex) {
		logger.debug("", ex);
        RequestContextExceptionHolder.setException(ex);
        return makeResult(HttpStatus.FORBIDDEN.value(), "抱歉，您没有操作权限！", null);
	}
	
	@ExceptionHandler(NotRoleException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
	public ReturnWrapper handleNotRoleException(NotRoleException ex) {
		logger.debug("", ex);
        RequestContextExceptionHolder.setException(ex);
        return makeResult(HttpStatus.FORBIDDEN.value(), "抱歉，您没有操作权限！", null);
	}
	
	@ExceptionHandler(SaTokenException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
	public ReturnWrapper handleSaTokenException(SaTokenException ex) { //其它SA-TOKEN异常由此接收
		logger.info("", ex);
        RequestContextExceptionHolder.setException(ex);
        return makeResult(HttpStatus.FORBIDDEN.value(), "操作受限！", null);
	}
}
